public class HuangNiu2
{
    /**
     * 初始库存量     *
     */
    Integer ticketNum = 8;

    //    public synchronized void reduce(int num)
    ////    {
    ////        //判断库存是否够用
    ////        if((ticketNum - num) >= 0)
    ////        {
    ////            try
    ////            {
    ////                TimeUnit.MILLISECONDS.sleep(200);
    ////            }
    ////            catch (InterruptedException e)
    ////            {
    ////                e.printStackTrace();
    ////            }
    ////            ticketNum -= num;
    ////            System.out.println(Thread.currentThread().getName() + "成功卖出" + num + "张，剩余" + ticketNum + "张票");
    ////        }
    ////        else
    ////        {
    ////            System.err.println(Thread.currentThread().getName() + "没有卖出" + num + "张，剩余" + ticketNum + "张票");
    ////        }
    ////    }

    public void reduceByLock(int num)
    {
        long startTime = System.currentTimeMillis();
        boolean flag = false;
        synchronized (ticketNum)
        {
            if((ticketNum - num) >= 0)
            {
                ticketNum -= num;
                flag = true;
            }
        }
        if(flag)
        {
            System.out.println(Thread.currentThread().getName() + "成功卖出" + num + "张，剩余" + ticketNum + "张票");
        }
        else
        {
            System.err.println(Thread.currentThread().getName() + "没有卖出" + num + "张，剩余" + ticketNum + "张票");
        }
        if(ticketNum == 0)
        {
            System.out.println("耗时" + (System.currentTimeMillis() - startTime) + "毫秒");
        }
    }

    public static void main(String[] args) throws InterruptedException
    {
        HuangNiu2 ticket = new HuangNiu2();//开启10个线程进行抢票，按理说应该有两个人抢不到票
        for(int i = 0; i < 10; i++)
        {
            new Thread(() -> ticket.reduceByLock(1), "用户" + (i + 1)).start();
        }
        Thread.sleep(1000L);
    }
}


